package XynLeen.GraduationProject.Fragments.Index;

import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.joda.time.LocalDate;
import org.litepal.crud.DataSupport;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import XynLeen.GraduationProject.BackPress.BaseFragment;
import XynLeen.GraduationProject.Bean.UserBean;
import XynLeen.GraduationProject.R;

public class FragmentA extends BaseFragment{

    UserBean mUser;

    SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private TextView mTvGoal;
    private TextView mTvLeft;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_a,container,false);

        mTvGoal=view.findViewById(R.id.tv_date_goal);
        mTvLeft=view.findViewById(R.id.tv_date_left);

        return view;

    }

    @Override
    public void onResume() {
        super.onResume();
        initData();

    }

    public void initData() {
        UserBean bean=DataSupport.find(UserBean.class,1);
        mTvGoal.setText(bean.getDate());
        //目标时间
        String goal=bean.getDate();
        //当前时间
        Date nowdate=new Date(System.currentTimeMillis());

        try {
            //要导入java.util.date的包,导入java.sql.date会出现格式转换错误
            Date goaldate=formatter1.parse(goal);
            long days=(goaldate.getTime()-nowdate.getTime())/(24*3600*1000);
            //long转换为int型
            int dayleft=new Long(days).intValue();
            //setText只能传字符类型,再将int转为String类型
            mTvLeft.setText(String.valueOf(dayleft));
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

}
